package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.Users;
import com.dao.*;
import com.util.ScreenUtils;
import com.util.SelectFarmer;
import com.util.SelectUsers;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class UpdateFarmerDialog extends JDialog{
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private String FarmerID;

    private JTextField IDField;
    private JTextField nameField;
    private JTextField passwordField;
    private JComboBox<String> FarmerGenderBox;
    private JTextField addressField;
    private JTextField phoneField;

    private ActionDoneListener listener;

    public UpdateFarmerDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener,String FarmerID){
        super(jf,title,isModel);
        this.listener  = listener;
        this.FarmerID = FarmerID;

        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装用户编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("农户编号：");
        IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装农户姓名
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("农户姓名：");
        nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装登录密码
        Box passwordBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("登陆密码：");
        passwordField = new JTextField(15);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(15));
        passwordBox.add(passwordField);

        //组装性别
        Box genderBox = Box.createHorizontalBox();
        JLabel genderLabel = new JLabel("  性     别：");
        FarmerGenderBox = new JComboBox<>();
        FarmerGenderBox.addItem("男");
        FarmerGenderBox.addItem("女");

        genderBox.add(genderLabel);
        genderBox.add(Box.createHorizontalStrut(20));
        genderBox.add(FarmerGenderBox);

        //组装家庭住址
        Box addressBox = Box.createHorizontalBox();
        JLabel addressLabel = new JLabel("家庭住址：");
        addressField = new JTextField(15);

        addressBox.add(addressLabel);
        addressBox.add(Box.createHorizontalStrut(15));
        addressBox.add(addressField);

        //组装联系电话
        Box phoneBox = Box.createHorizontalBox();
        JLabel phoneLabel = new JLabel("联系电话：");
        phoneField = new JTextField(15);

        phoneBox.add(phoneLabel);
        phoneBox.add(Box.createHorizontalStrut(15));
        phoneBox.add(phoneField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("修改");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id = Integer.valueOf(FarmerID); //要修改用户的编号
                //获取用户的录入
                int farmerId = Integer.parseInt(IDField.getText().trim()); //添加的用户编号
                String Farmername = nameField.getText().trim();
                String Password = passwordField.getText().trim();
                String Gender = (String) FarmerGenderBox.getSelectedItem();
                String Address = addressField.getText().trim();
                String PhoneNumber = phoneField.getText().trim();
                //将数据添加进数据库
                Farmer farmer = new Farmer(farmerId,Farmername,Password,Gender,Address,PhoneNumber);
                new UpdateFarmer().updateFarmer(farmer,id);
                JOptionPane.showMessageDialog(jf, "修改成功!");
                listener.done(null);
                dispose(); // 关闭添加窗口
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(genderBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(addressBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(phoneBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        requestData();

        this.add(hBox);
    }
    //请求数据
    public void requestData(){
        List<Farmer> list = new SelectFarmer().getFarmerData();
        for(Farmer f:list)
        {
            if(Integer.toString(f.getFarmerId()).equals(FarmerID)){
                IDField.setText(FarmerID);
                nameField.setText(f.getFarmerName());
                passwordField.setText(f.getPassword());
                FarmerGenderBox.setSelectedItem(f.getGender());
                addressField.setText(f.getAddress());
                phoneField.setText(f.getPhoneNumber());
                break;
            }
        }
    }

}